<template>
  <div>
    <el-dialog v-bind="$attrs" v-on="$listeners" @open="onOpen" @close="onClose" title="Dialog Titile">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="图片" prop="imageUrl" required>
          <el-upload ref="imageUrl" :file-list="imageUrlfileList" :action="imageUrlAction"
            :before-upload="imageUrlBeforeUpload" list-type="picture" accept="image/*">
            <el-button size="small" type="primary" icon="el-icon-upload">上传图片</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="位置" prop="postion">
          <el-input v-model="formData.postion" placeholder="请输入位置" :maxlength="60" clearable></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="field104">
          <el-input-number v-model="formData.field104" placeholder="排序"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  inheritAttrs: false,
  components: {},
  props: [],
  data() {
    return {
      formData: {
        imageUrl: null,
        postion: undefined,
        field104: 0,
      },
      rules: {
        postion: [{
          required: true,
          message: '请输入位置',
          trigger: 'blur'
        }],
        field104: [{
          required: true,
          message: '排序',
          trigger: 'blur'
        }],
      },
      imageUrlAction: 'https://jsonplaceholder.typicode.com/posts/',
      imageUrlfileList: [],
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    onOpen() {},
    onClose() {
      this.$refs['elForm'].resetFields()
    },
    close() {
      this.$emit('update:visible', false)
    },
    handelConfirm() {
      this.$refs['elForm'].validate(valid => {
        if (!valid) return
        this.close()
      })
    },
    imageUrlBeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 100
      if (!isRightSize) {
        this.$message.error('文件大小超过 100MB')
      }
      let isAccept = new RegExp('image/*').test(file.type)
      if (!isAccept) {
        this.$message.error('应该选择image/*类型的文件')
      }
      return isRightSize && isAccept
    },
  }
}

</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}

</style>
